package mysql

import "gorm.io/gorm"

type Coupons struct {
	gorm.Model
	UserId        int     `gorm:"type:int;comment:用户id"`
	CouponsPrice  float64 `gorm:"type:decimal(10,2);comment:该优惠扣减的价钱"`
	Name          string  `gorm:"type:varchar(20);comment:优惠卷名称"`
	CouponsStatus int     `gorm:"type:int;default:2;comment:优惠卷状态：1-已过期，2-未过期，3-已使用"`
}

func (Coupons) TableName() string {
	return "coupons"
}

func (c *Coupons) Create() error {
	return DB.Create(&c).Error
}

func (c *Coupons) Find(userId int) (result Coupons, err error) {
	err = DB.Where("user_id=?", userId).Find(&result).Error
	if err != nil {
		return Coupons{}, err
	}
	return result, nil
}

func (c *Coupons) Finds() (result []Coupons, err error) {
	err = DB.Find(&result).Error
	if err != nil {
		return nil, err
	}
	return result, nil
}
